//
//  BookDetailController.m
//  悦读
//
//  Created by dllo on 15/5/22.
//  Copyright (c) 2015年 贾建兴. All rights reserved.
//

#import "BookDetailController.h"
#import "SANetWorkingTask.h"
#import "IntroTableViewCell.h"
#import "IntroduceTableViewCell.h"
#import "BookReadController.h"
#import "CollectClass.h"
@interface BookDetailController ()<UITableViewDataSource, UITableViewDelegate,IntroTableViewCellDelegate>

@property (nonatomic, retain) UITableView *detailTable;
@property (nonatomic, retain) NSMutableDictionary *detailDic;
@property(nonatomic, retain)NSMutableArray *typeArray;

@end

@implementation BookDetailController


- (void)dealloc
{
    [_detailTable release];
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.detailDic = [NSMutableDictionary dictionary];
        self.typeArray = [NSMutableArray array];
    }
    return self;
}

- (void)loadView
{
    [super loadView];

 
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(action)];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.rightBarButtonItem = right;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"书籍详情";
    NSDictionary *dic = @{@"version":@"1.3", @"bookid":self.str};
    
    __block BookDetailController *bookDetail = self;
    
    //   
    [SANetWorkingTask request:@"http://vip.hentr.com/zhuibook/api/zhuibook/GetBookInfo.do" parmaters:dic block:^(NSDictionary *result) {
        NSDictionary *dic = [result objectForKey:@"response"];
        bookDetail.detailDic = [dic objectForKey:@"ReturnData"];
        
        [bookDetail.detailTable reloadData];
    }];
    
    
    
    NSDictionary *dic1 = @{@"bookid":self.str};
    [SANetWorkingTask request:@"http://vip.hentr.com/zhuibook/api/zhuibook/getBookCategoryList.do" parmaters:dic1 block:^(NSDictionary *result) {
        [bookDetail.typeArray addObjectsFromArray:[[result objectForKey:@"response"] objectForKey:@"ReturnData"]];
        [bookDetail.detailTable reloadData];
    }];

    [self createDetailTable];
}

// 保存
- (void)action
{
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:[self.detailDic objectForKey:@"bookid"] forKey:@"bookid"];
        [dic setObject:[self.detailDic objectForKey:@"bookName"] forKey:@"bookName"];
        [dic setObject:[self.detailDic objectForKey:@"imageUrl"] forKey:@"imageUrl"];
        [CollectClass writeArray:dic];
    

}
#pragma mark 网络请求和数据解析

#pragma mark 创建tableView
- (void)createDetailTable
{
    
    self.detailTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    
    self.detailTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.detailTable.bounces = NO;
    
    self.detailTable.delegate = self;
    self.detailTable.dataSource = self;
    [self.view addSubview:self.detailTable];
    [self.detailTable release];
    
    self.detailTable.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
}



#pragma  mark 执行tableView的协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        static NSString *cellIndentify = @"jia";
        IntroTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentify];
        if (!cell) {
            cell = [[IntroTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentify];
        }
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
       //给书籍详情的图片,作者等信息赋值
        [cell getDataWithDic:self.detailDic array:self.typeArray];
        return cell;
    }
    
    if (indexPath.row == 1) {
        
        static NSString *cellIndentify = @"jian";
        IntroduceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentify];
        if (!cell) {
            cell = [[IntroduceTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentify];
        }

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.introduceLabel.text = [self.detailDic objectForKey:@"intro"];
        
        return cell;

        
        
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 160;
    }
    
    if (indexPath.row == 1) {
        //设置小说简介内容的自适应高度
        NSString *str = [self.detailDic objectForKey:@"intro"];
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGRect rect = [str boundingRectWithSize:CGSizeMake(350, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + 10;
    }
    return 100;
}


#pragma mark - cell里的协议实现
//点击阅读本书,进入阅读界面
- (void)toRead
{
    
    NSString *isPlay = [self.detailDic objectForKey:@"isPay"];
    BookReadController *bookRead = [[BookReadController alloc] init];
    bookRead.sourceId= isPlay;
    [self.navigationController pushViewController:bookRead animated:YES];
    
    [bookRead release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
